package cjlu.ea.h.common;

import cjlu.ea.h.entity.SUser;
import cjlu.ea.h.service.SUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Component
public class AuthUtil {

    @Autowired
    SUserService userService;


    RedisTemplate redisTemplate;

    @Resource
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public String getAuthentications(HttpServletRequest request){
        TokenUtils tokenUtils = new TokenUtils();
        String token = request.getHeader("token");
        System.out.println("从请求头获取token:"+token);
        String username = tokenUtils.getUserFromToken(token);
        if(StringUtils.isEmpty(token)){
            return null;
        }
        //获取用户信息
        String roles = (String)redisTemplate.opsForValue().get("UserRoles"+ username);

        return roles;
    }
}
